﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.MobileControls;

namespace Shop.WapController.Controls
{
    /// <summary>
    /// DataPager adapter for wml
    /// </summary>
    public class HtmlDataPagerAdapter : System.Web.UI.MobileControls.Adapters.HtmlControlAdapter
    {

        private DataPager _control;

        public override void OnInit(EventArgs e)
        {
            _control = this.Control as DataPager;            
            base.OnInit(e);
        }

        public override void Render(System.Web.UI.MobileControls.Adapters.HtmlMobileTextWriter writer)
        {
            //output recordcount to client
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "hidden");
            writer.AddAttribute(HtmlTextWriterAttribute.Name, _control.UniqueID);
            writer.AddAttribute(HtmlTextWriterAttribute.Value, _control.PageIndex.ToString() + "," + _control.PageCount.ToString());
            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag();

            //output pager ui
            if (!string.IsNullOrEmpty(_control.CssClass))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, _control.CssClass);
            }
            writer.RenderBeginTag(_control.Layout);     //begin pager container                   

            if (_control.Layout == HtmlTextWriterTag.Table)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);    //begin pager tr
                writer.RenderBeginTag(HtmlTextWriterTag.Td);    //begin pager td
            }

            //previous button
            if (_control.PreviousItem != null)
            {
                if (_control.PageIndex > 1)
                {
                    _control.PreviousItem.CanLink = true;
                    _control.PreviousItem.PageIndex = _control.PageIndex - 1;
                }
                else
                {
                    _control.PreviousItem.CanLink = false;
                }

                this.ComposeNavigationItem(writer, _control.PreviousItem);
            }

            //next button
            if (_control.NextItem != null)
            {
                if (_control.PageIndex < _control.PageCount)
                {
                    _control.NextItem.CanLink = true;
                    _control.NextItem.PageIndex = _control.PageIndex + 1;
                }
                else
                {
                    _control.NextItem.CanLink = false;
                }

                _control.NextItem.CanLink = _control.PageIndex < _control.PageCount;
                this.ComposeNavigationItem(writer, _control.NextItem);
            }

            if (_control.Layout == HtmlTextWriterTag.Table)
            {
                writer.RenderEndTag();      //end td pager navigation
                writer.RenderEndTag();      //end tr pager navigation

                writer.RenderBeginTag(HtmlTextWriterTag.Tr);    //begin tr pager summary
                //pager summary 
                writer.RenderBeginTag(HtmlTextWriterTag.Td);    //begin summary format
                if (string.IsNullOrEmpty(_control.SummaryFormat))
                {
                    _control.SummaryFormat = string.Format(DataPager.DEFAULT_SUMMARY_FORMAT, _control.PageIndex, _control.PageCount, _control.RecordCount);
                }
                else
                {
                    _control.SummaryFormat = string.Format(_control.SummaryFormat, _control.PageIndex, _control.PageCount, _control.RecordCount);
                }

                writer.Write(_control.SummaryFormat);   //writer pager summary
                writer.RenderEndTag();    //end td pager summary
                writer.RenderEndTag();    //begin tr pager summary
            }

            writer.RenderEndTag();      //end pager container            
        }

        private void ComposeNavigationItem(HtmlTextWriter writer, PagerNavigationItem navigation)
        {
            if (navigation.CanLink)
            {
                if (!string.IsNullOrEmpty(navigation.CssClass))
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, navigation.CssClass);
                }
                string js = string.Format("javascript:{0};", _control.Page.ClientScript.GetPostBackEventReference(_control, navigation.PageIndex.ToString()));
                writer.AddAttribute(HtmlTextWriterAttribute.Href, js);
                writer.RenderBeginTag(HtmlTextWriterTag.A);     //begin a
                if (string.IsNullOrEmpty(navigation.ImageUrl))
                {
                    writer.Write(navigation.Text);
                }
                else
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Src, navigation.ImageUrl);
                    writer.AddAttribute(HtmlTextWriterAttribute.Alt, navigation.AlternateText);
                    writer.RenderBeginTag(HtmlTextWriterTag.Img);   //begin img
                    writer.RenderEndTag();  //end img
                }
                writer.Write(" ");
                writer.RenderEndTag();  //end a
                writer.Write(" ");
            }
            else
            {
                writer.Write(" ");
                writer.Write(navigation.Text);
                writer.Write(" ");
            }
        }

    }
}
